package tyw.com.userms.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Telephony;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import tyw.com.userms.R;
import tyw.com.userms.utils.DbManager;
import tyw.com.userms.model.User;
import tyw.com.userms.adapter.UserAdapter;
import tyw.com.userms.utils.SpUtil;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    DbManager dbManager;

    RecyclerView listUser;

    UserAdapter userAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dbManager = new DbManager(this);
        boolean firstLoad = SpUtil.getBoolean(this, "firstLoad", true);

        //第一次初始化 添加十个用户数据
        if(firstLoad){

            SpUtil.putBoolean(this, "firstLoad", false);

            User user = new User("宋倩", "13777477649");
            dbManager.addUser(user);

            User user2 = new User("王二", "13777477640");
            dbManager.addUser(user2);

            User user3 = new User("王三", "13777477641");
            dbManager.addUser(user3);

            User user4 = new User("赵四", "13777477641");
            dbManager.addUser(user4);

            User user5 = new User("张五", "13777477641");
            dbManager.addUser(user5);

            User user6 = new User("钱六", "13777477646");
            dbManager.addUser(user6);

            User user7 = new User("孙七", "13777477647");
            dbManager.addUser(user7);

            User user8 = new User("周八", "13777477648");
            dbManager.addUser(user8);

            User user9 = new User("吴九", "13777477649");
            dbManager.addUser(user9);

            User user10 = new User("宋十", "13777477640");
            dbManager.addUser(user10);


        }



        findViewById(R.id.btn_add).setOnClickListener(this);
        findViewById(R.id.btn_delete).setOnClickListener(this);
        findViewById(R.id.btn_groupsend).setOnClickListener(this);
        findViewById(R.id.btn_clear).setOnClickListener(this);
        listUser = findViewById(R.id.list_user);


        List<User> users = dbManager.getUserList();
        listUser.setLayoutManager(new LinearLayoutManager(this));
        userAdapter = new UserAdapter(this, users);
        listUser.setAdapter(userAdapter);

    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {


            case R.id.btn_add:

                Intent intent = new Intent(this, AddActivity.class);
                startActivityForResult(intent, 0);

                break;
            case R.id.btn_delete:

                List<User> selectUser = userAdapter.getSelectUser();
                for (int i = 0; i < selectUser.size(); i++) {
                    dbManager.deleteUser(selectUser.get(i));
                }
                List<User> userList2 = dbManager.getUserList();
                userAdapter = new UserAdapter(this, userList2);
                listUser.setAdapter(userAdapter);
                Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_groupsend:

                List<User> selectUserT = userAdapter.getSelectUser();
                List<String> phone = new ArrayList<>();

                for (int i = 0; i <selectUserT.size() ; i++) {
                    phone.add(selectUserT.get(i).phone);
                }

                if(phone.size() > 0){

                    sendSms(this,"",phone);
                }

                break;
            case R.id.btn_clear:


                userAdapter.initUser();
                break;
        }
    }


    public static void sendSms(Context context, String text, List<String> numbers) {
        String numbersStr = "";
        String symbol = "Samsung".equalsIgnoreCase(Build.MANUFACTURER) ? ",": ";";
        if (numbers != null && !numbers.isEmpty()) {
            numbersStr = TextUtils.join(symbol, numbers);
        }
        Uri uri = Uri.parse("smsto:" + numbersStr);

        Intent intent = new Intent();
        intent.setData(uri);
        intent.putExtra("sms_body", text);
        intent.setAction(Intent.ACTION_SENDTO);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(context);
            if (defaultSmsPackageName != null) {
                intent.setPackage(defaultSmsPackageName);
            }
        }
        if (!(context instanceof AppCompatActivity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        try {
            context.startActivity(intent);
        } catch (Exception e) {
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {

            List<User> userList = dbManager.getUserList();
            userAdapter = new UserAdapter(this, userList);
            listUser.setAdapter(userAdapter);
        }
    }
}
